/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Form;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;

/**
 *
 * @author Kvest
 */
public class EfirOrdersList extends Form implements ActionListener
{
    public static final int SHOW_ID = 1;
    public static final int BACK_ID = 2;
    public static final int TEN_MINUTES_ID = 3;

    private Command show;
    private Command back;
    private FormAction listener;

    //Список заказов
    private ThumbList ordersList;
    
    private String centerButtonText = "";

    public EfirOrdersList(FormAction listener)
    {
        super("Эфир");
        this.listener = listener;

        init();
    }

    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        addCommandListener(this);

        back= new Command("Назад", BACK_ID);
        addCommand(back);

        show = new Command("Показать", SHOW_ID);
        addCommand(show);

        //Создаем и настраиваем список
        ordersList = new ThumbList();
        ordersList.setFireOnClick(false);
        ordersList.setNeedFireOnRelease(false);
        ordersList.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        render.setTickerEnabled(ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.KIROVOGRAD_TYPE);
        render.getStyle().setPadding(Component.BOTTOM, 0);
        render.getStyle().setPadding(Component.TOP, 0);
        ordersList.setRenderer(render);
        ordersList.getStyle().setPadding(Component.BOTTOM, 0);
        ordersList.getStyle().setPadding(Component.TOP, 0);
        ordersList.setSelectCommandText("");
        ordersList.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                if (listener != null && ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.CHERNOVTSI_TYPE)
                {                   
                    listener.commandAction(TEN_MINUTES_ID);
                }
            }
        });

        addComponent(BorderLayout.CENTER,ordersList);
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }

    public void setTakingOrderCommand(boolean takeOrder)
    {
        if (takeOrder)
        {
            if (ViewManager.getViewManager().getFunctionMenuType() == FunctionMenu.CHERNOVTSI_TYPE)
            {
//                getSoftButton(0).setText("Взять");
//                ordersList.setSelectCommandText("Взять");    
                getSoftButton(0).setText("");
                ordersList.setSelectCommandText("");
            }
            else
            {
                getSoftButton(0).setText(ViewManager.getViewManager().getTenMinutesText() + "мин.");
                ordersList.setSelectCommandText(ViewManager.getViewManager().getTenMinutesText() + "мин.");
            }
        }
        else
        {
            getSoftButton(0).setText("");
            ordersList.setSelectCommandText("");
        }
        
        centerButtonText = ordersList.getSelectCommandText();
    }

    public CellItem getSelectedItem()
    {
        return (CellItem)ordersList.getSelectedItem();
    }

    public void clear()
    {
        while (ordersList.size() > 0)
        {
            ordersList.getModel().removeItem(0);
        }
    }

    public void add(CellItem item)
    {
        ordersList.getModel().addItem(item);
    }

    public void setSelectedItem(CellItem item)
    {
        try
        {
            ordersList.setSelectedItem(item);
        }catch(Exception ex){}
    }

    public int size()
    {
        return ordersList.size();
    }

    public void unselectAll()
    {
         ordersList.getModel().setSelectedIndex(-1);
         
         ordersList.setSelectCommandText("");
         getSoftButton(0).setText("");
         show.setCommandName("");
         getSoftButton(2).setText("");
    }
    
    public void keyPressed(int keyCode)
    {
        int oldSelectedIndex = ordersList.getSelectedIndex();
        
        int game = Display.getInstance().getGameAction(keyCode);
        if(ordersList.size() == 0 && (game == Display.GAME_DOWN || game == Display.GAME_UP)) 
        {
            return;
        }
        
        super.keyPressed(keyCode);

        if (oldSelectedIndex == -1 && ordersList.getSelectedIndex() != -1)
        {    
            ordersList.setSelectCommandText(centerButtonText);
            getSoftButton(0).setText(centerButtonText);
            show.setCommandName("Показать");
            getSoftButton(2).setText("Показать");
            
            repaint();
        }
    }
    
    public void pointerPressed(int x, int y)
    {
        int oldSelectedIndex = ordersList.getSelectedIndex();
        
        super.pointerPressed(x, y);

        if (oldSelectedIndex == -1 && ordersList.getSelectedIndex() != -1)
        {    
            ordersList.setSelectCommandText(centerButtonText);
            getSoftButton(0).setText(centerButtonText);
            show.setCommandName("Показать");
            getSoftButton(2).setText("Показать");
            
            repaint();
        }
    }
    
//    public void setSelectedIndex(int index, boolean scrollToSelected)
//    {
//        try
//        {
//            ordersList.setSelectedIndex(index,scrollToSelected);
//        }catch(Exception ex){}
//    }
}
